@echo off
echo ========================================
echo PPH目标值节拍字段修复更新脚本
echo ========================================
echo.

echo [1/5] 创建备份...
set BACKUP_DIR=app\frontend\dist.backup.%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%%time:~6,2%
set BACKUP_DIR=%BACKUP_DIR: =0%
if exist app\frontend\dist (
    xcopy app\frontend\dist %BACKUP_DIR%\ /E /I /Q
    echo 备份已创建: %BACKUP_DIR%
) else (
    echo 警告: dist目录不存在，跳过备份
)

echo.
echo [2/5] 停止PM2服务...
call pm2 stop all
if %errorlevel% neq 0 (
    echo 警告: PM2停止失败，继续执行...
)

echo.
echo [3/5] 重新构建前端...
cd app\frontend
call npm run build
if %errorlevel% neq 0 (
    echo 错误: 前端构建失败！
    cd ..\..
    goto :error
)
cd ..\..

echo.
echo [4/5] 重启PM2服务...
call pm2 restart all
if %errorlevel% neq 0 (
    echo 错误: PM2重启失败！
    goto :error
)

echo.
echo [5/5] 检查服务状态...
call pm2 status

echo.
echo ========================================
echo 更新完成！
echo ========================================
echo.
echo 请执行以下验证步骤：
echo 1. 打开浏览器访问PPH系统
echo 2. 强制刷新页面 (Ctrl+Shift+R)
echo 3. 进入"PPH目标值管理"
echo 4. 测试节拍字段小数输入功能
echo.
echo 如果有问题，可以恢复备份：
echo xcopy %BACKUP_DIR%\* app\frontend\dist\ /E /Y
echo.
goto :end

:error
echo.
echo ========================================
echo 更新失败！
echo ========================================
echo 请检查错误信息并手动处理
echo.

:end
pause
